﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmHolidayList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int HolidayID;
        HolidayClient wcfHoliday;
        HolidayInfo _HolidayInfo;
        RoleRightInfo listRole;
        bool _loadedData=false;

        public override void Show_Form()
        {
            this.Show();
        }

        public frmHolidayList()
        {
            this.KeyPreview = true;
            InitializeComponent();
           // LoadLanguage();
            wcfHoliday = Service.Holiday();
            BuildVoucherGrid();
            UISetting.LoadYears(ref cboYear);
            cboYear.Text = DateTime.Now.Year.ToString();

            listRole = UIParamater.Quyen_NgayNghiTrongNam;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole._delete)
                btnDel.Enabled = false;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            LoadData();
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
                
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "Holiday_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Holiday_Date", UpdateUI.GetString("ngay"), "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Holiday_Note", UpdateUI.GetString("ghichu"), "", 300, true, true);
        }
        private void LoadData()
        {
            int _year = 0;
            _year = Convert.ToInt32(cboYear.Text);
            string keyWord = "";
            if (txtKeyWord.Text.Trim().Contains("'"))
                keyWord = txtKeyWord.Text.Trim().Replace("'", "''");
            else
                keyWord = txtKeyWord.Text.Trim();
            DataSet _ds = wcfHoliday.Holiday_GetList(out itemCount, keyWord, currPage, pageSize, _year);
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["Holiday_ID"].Value = _ds.Tables[0].Rows[i]["Holiday_ID"].ToString();
                dgvList.Rows[i].Cells["Holiday_Date"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Holiday_Date"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["Holiday_Note"].Value = _ds.Tables[0].Rows[i]["Holiday_Note"].ToString();
            }
            _loadedData = true;

        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmHolidayInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                HolidayID = Convert.ToInt32(dgvList.CurrentRow.Cells["Holiday_ID"].Value);
                if (wcfHoliday.Holiday_Using(HolidayID) != 0)
                {
                    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfHoliday.Holiday_Delete(HolidayID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {

                    HolidayID = Convert.ToInt32(dgvList.CurrentRow.Cells["Holiday_ID"].Value);
                    Form f = new frmHolidayInfo(HolidayID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmHolidayList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
            }
        }

        private void btnNewHoliday_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditHoliday_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDeleteHoliday_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void frmHolidayList_Activated(object sender, EventArgs e)
        {
            //LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["Holiday_Date"].HeaderText = UpdateUI.GetString("ngay");
                dgvList.Columns["Holiday_Note"].HeaderText = UpdateUI.GetString("ghichu");
            }
        }

        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                btnHoliday_Click(null, null);
                currPage = 0;
                LoadData();
            }
        }

        private void btnHoliday_Click(object sender, EventArgs e)
        {
            DateTime[] _holiday = null;
            _holiday = new DateTime[] { UISetting.GetDate("01/01/" + cboYear.Text), 
                UISetting.GetDate("30/04/" + cboYear.Text), 
                UISetting.GetDate("01/05/" + cboYear.Text),
                UISetting.GetDate("02/09/" + cboYear.Text)};
            _HolidayInfo = new HolidayInfo();
            for (int i = 0; i < _holiday.Length; i++)
            {
                if (i == 0)
                    _HolidayInfo.Note = "Tết dương lịch";
                else if (i == 1)
                    _HolidayInfo.Note = "Ngày giải phóng miền nam";
                else if (i == 2)
                    _HolidayInfo.Note = "Ngày quốc tế lao động";
                else
                    _HolidayInfo.Note = "Ngày quốc khánh";

                if (!wcfHoliday.Holiday_Exist(_holiday[i]))
                {
                    _HolidayInfo.Date = _holiday[i];
                    wcfHoliday.Holiday_Insert(_HolidayInfo);
                }
            }
        }

        private void frmHolidayList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfHoliday.Close();
        }
    }
}
